<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Input;
use Session;
//引入会员表
use App\Admin\Member;
use Auth;
use DB;

use Mail;


class MemberController extends Controller
{
	//renyu
    //移动支付的
  public function pay(){
    // $data = \App\Admin\Huiyuan::find(1);
     $data = \App\Admin\Huiyuan::find(Input::get('id'));
    //付款入库
    //3、使用微信付款
    require_once "./wx/lib/WxPay.Api.php";
    require_once "./wx/example/WxPay.NativePay.php";
    require_once './wx/example/log.php';
    
    $notify = new \NativePay();
    $input = new \WxPayUnifiedOrder();
    $input->SetBody("丰城购买：" . $data -> hui_name);
    $input->SetAttach("test");
    $input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
    $input->SetTotal_fee($data -> hui_price * 0.001 * 1000);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 1800));
    $input->SetGoods_tag("test");
    $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($data -> id);
    $result = $notify->GetPayUrl($input);
    // dd($result);
    $url2 = $result["code_url"];
    // dd($url2);
    //4、输出二维码
    echo "<img alt='模式二扫码支付' src='http://paysdk.weixin.qq.com/example/qrcode.php?data=". urlencode($url2) . " ' style='width:150px;height:150px;'/>";
    // die;
  }


    public function index(){

    	if(Input::method() == 'POST'){

        $code = input::get("code");

        if($code == Session::get("smscode")){
            //验证过后，删除掉session中的验证码
            Session::forget('smscode');
            $pwd = bcrypt(Input::get('password'));
        		//验证验证码
        		$data = input::except('_token',"password","password2","code");
        	  $data['password'] = $pwd;
            $data['created_at'] = date('Y-m-d H:i:s');
            $result = \App\Admin\Member::insert($data);
            // file_get_contents("/home/member/memLogin");

            // die();
            return view("/home/member/memLogin");

        }else{
          echo "验证码错误";
          header("Refresh:3;url=/home/member/index");

         
          
          // die;
          // return history("验证码错误");
          // echo json_encode(2);
          // die;

        }
        // return $result ? '1' : '0';

    	}else{

    		return view('home.member.index');

    	}
        
    }

    //发送短信的验证码
  	public function sendCode(){
  	//接口的请求地址
    // 请求示例：http://v.juhe.cn/sms/send?mobile=手机号码&tpl_id=短信模板
    // ID&tpl_value=%23code%23%3D654654&key=接口Key
  		$mobile = Input::get('mobile');
  		$url = "http://v.juhe.cn/sms/send?mobile=$mobile&tpl_id=53135&key=d36c1d9829185050b5ac21d5beafa4a8&tpl_value=";
  		$code = rand(100000,999999);
  		Session::put('smscode', $code);
  		// session('smscode',$code);//记录到session中
  		$str = "#code#=$code";
  		// //使用php内置函数urlencode
  		// $str = urlencode($str);
  		$str = rawurlencode($str);
  		// //重新组装地址
  		$url .= $str;
  		// echo $url;
  		//return $url;
  		$data = file_get_contents($url);
		  echo $data;
  		// $data = requestUrl($url,false);
  		// //输出数据
  		// echo $data;
  	}
  	
  	
	/*sunyu*/
	//展示登录页面
    public function memLogin(Request $request){
    	
    	if(Input::method() == 'POST') {
      		$data = $request -> only('username','password');
		
       		 $data['status'] = '2';
        	//开始用户信息的认证
        	$result = Auth::guard('member') -> attempt($data);
        
//     		 $result = DB::table('member')->where('username',$data['username'])->where('password',$data['password'])->where('status',$data['status'])->get();
        	

	      return $result ? '1' : '0';
	
    	}else {	
    		
    		if(Auth::guard('member')->user()){

    			return view('home.member.login'); 
    		}else{
    			return view('home.member.memLogin');  	
    		}		
					
    	}
    	
      
    }
    
    //退出
    public function logout() {
    	
    	Auth::guard('member')->logout();
    	return redirect('/');
    	
    }
    
    //切换用户
    public function qiehuan() {
    	
    	Auth::guard('member')->logout();
    	return redirect('/home/member/memLogin');
    	
    }
    
    //修改密码
    public function editPassword() {
    	
    	if(Input::method() == 'POST') {

    		$id = $_POST['id'];
    		$password = $_POST['password'];
    		$password2 = $_POST['password2'];
    		$code = $_POST['code'];
    		
//  		 && $code == Session::get("code")
    		if($password == $password2 && $code == Session::get("code")) {
    			
    			Session::forget('code');
    			
    			$password = bcrypt($password);
    			
    			$result = Member::where('id',$id)->update(['password'=>$password]);

    		}
    		
    		
//  		return $code;
    		return $result ? '1' : '0';
    		
    	}else {
    
    		return view('/home/member/editPassword');
    	}
    	
    }
    
    //发送验证邮件
    public function mailTest() {
    		
		
		
		$name='峰城贸易';
		
		$code = rand(1000,9999);
		
		Session::put('code', $code);
 
		$flag= Mail::raw($code,function($message) {
		$email = $_POST['email'];
		$to=$email;
		 
		$message->to($to)->subject('峰城贸易修改密码验证码');
		 
		});
		 
		if(!$flag){
		 
			return '1';
		 
		}else{
		 
			return '0';
		 
		}
		
		
    }
			


}


